<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID" prop="memberId">
        <el-input
            v-model="queryParams.memberId"
            placeholder="请输入用户ID"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务ID" prop="taskId">
        <el-input
            v-model="queryParams.taskId"
            placeholder="请输入任务ID"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务进度" prop="progress">
        <el-input
            v-model="queryParams.progress"
            placeholder="请输入任务进度"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="info"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
        >导出
        </el-button>
      </el-col>

    </el-row>

    <el-table v-loading="loading" :data="member_task_logList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="#" align="center" prop="id"/>
      <el-table-column label="用户ID" align="center" prop="memberId"/>
      <el-table-column label="任务ID" align="center" prop="taskId"/>
      <el-table-column label="任务进度" align="center" prop="progress"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              v-has-permission="['ums:member_task_log:edit']"
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
              v-has-permission="['ums:member_task_log:remove']"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="pagination.total > 0"
        :total="pagination.total"
        :page.sync="pagination.pageNum"
        :limit.sync="pagination.pageSize"
        @pagination="handleQuery"
    />
    <!-- 添加或修改用户任务记录对话框 -->
    <el-dialog :title="dialog.title" :visible.sync="dialog.visible" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="memberId">
          <el-input v-model="form.memberId" placeholder="请输入用户ID"/>
        </el-form-item>
        <el-form-item label="任务ID" prop="taskId">
          <el-input v-model="form.taskId" placeholder="请输入任务ID"/>
        </el-form-item>
        <el-form-item label="任务进度" prop="progress">
          <el-input v-model="form.progress" placeholder="请输入任务进度"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {downloadFile} from '@/utils'
import {
  getMember_task_log,
  addMember_task_log,
  delMember_task_log,
  exportMember_task_log,
  listMember_task_log,
  updateMember_task_log
} from '@/api/ums/member_task_log'

export default {
  name: 'Member_task_log',
  components: {},
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      pagination: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      // 用户任务记录表格数据
      member_task_logList: [],
      // 弹出层
      dialog: {
        title: undefined,
        visible: false,
        type: undefined // type 操作类型：1-新增 2-修改
      },
      // 查询参数
      queryParams: {
        memberId: null,
        taskId: null,
        progress: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created () {
    this.getList()
  },
  methods: {
    /** 查询用户任务记录列表 */
    getList () {
      this.loading = true
      Object.keys(this.queryParams).forEach(key => {
        this.queryParams[key] === '' ? this.queryParams[key] = null : false
      })
      this.queryParams.pageNum = this.pagination.pageNum
      this.queryParams.pageSize = this.pagination.pageSize
      listMember_task_log(this.queryParams).then(response => {
        const {data} = response
        this.member_task_logList = data.list
        this.pagination.total = data.totalCount
        this.loading = false
      })
    },
    // 取消按钮
    cancel () {
      this.reset()
      this.closeDialog()
    },
    handleReset () {
      this.pagination = {
        pageNum: 1,
        pageSize: 10,
        total: 0
      }
      this.queryParams = {
        memberId: null,
        taskId: null,
        progress: null
      }
      this.handleQuery()
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        memberId: null,
        taskId: null,
        progress: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    resetForm (val) {
      if (this.$refs[val]) {
        this.$refs[val].resetFields()
      }
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset()
      this.dialog = {
        title: '添加用户任务记录',
        visible: true,
        type: 'add'
      }
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset()
      this.dialog = {
        title: '修改用户任务记录',
        visible: true,
        type: 'edit'
      }
      const id = row.id || this.ids
      getMember_task_log(id).then(response => {
        this.form = response.data
        // this.open = true;
        // this.title = "修改用户任务记录";
      })
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.dialog.type === 'edit') {
            updateMember_task_log(this.form).then(response => {
              this.$message.success('修改成功')
              this.closeDialog()
              this.getList()
            })
          } else {
            addMember_task_log(this.form).then(response => {
              this.$message.success('新增成功')
              this.closeDialog()
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id ? [row.id] : this.ids
      this.$confirm('是否确认删除用户任务记录编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delMember_task_log(ids)
      }).then(() => {
        this.getList()
        this.$message.success('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport () {
      const queryParams = this.queryParams
      delete queryParams.pageNum
      delete queryParams.pageSize
      this.$confirm('是否确认导出所有用户任务记录数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return exportMember_task_log(queryParams)
      }).then(response => {
        downloadFile(response.data, 'client', 'xls')
      })
    },
    // handleSizeChange(val) {
    //     this.queryParams.pageSize = val
    //     this.getList()
    // },
    // handleCurrentChange(val) {
    //     this.queryParams.pageNum = val
    //     this.tableList = []
    //     this.getList()
    // },
    closeDialog () {
      this.dialog = {
        title: undefined,
        visible: false,
        type: undefined
      }
    }
  }
}
</script>
